**************************
**************************
* Matching
**************************
**************************

set more off
use "${FINALDATA}\immo_panel.dta", clear

cap drop __00*

drop if time >= monthly("2015 10","YM")
cd "$GRAPHS/"


***************************
***************************
* Variablen für Grafiken generieren
***************************
***************************

** Define time relative to disclosure --> time_disclosure
bysort geo: gen help = time if epc_change == 1
bysort geo (time): egen help2 = max(help)
gen time_disclosure = time - help2 if help2 != .  /* t = 0: disclosure */
drop help help2

** Time when disclosure occurs
gen logkaufp_firstobs = log(kaufpreis_firstobs)
gen logkaufp_diff = logkaufp - logkaufp_firstobs

** Month of disclosure
gen epc_change_month = time if epc_change == 1
bysort geo (epc_change_month): replace epc_change_month = epc_change_month[_n-1] if _n>1

** Indicator variable for observations close to the cutoff
bysort geo: egen min_time = min(time)
bysort geo: egen max_time = max(time)
gen around_mai14 = 0
replace around_mai14 = 1 if min_time < `=monthly("2014 05","YM")' & max_time > `=monthly("2014 05","YM")'

** Time when disclosure occurs
gen epc_change_time = time*epc_change 
gsort geo -epc_change_time
bysort geo: replace epc_change_time = epc_change_time[_n-1] if _n > 1

gen epc_post = epc
replace epc_post = 0 if epc_change_time < monthly("2014 05","YM")

** Complier: Beobachtungen, die den Ausweis erst später angeben
gen complier = epc_change_time != 0

sort geo time


***************************************************
* Code variables
***************************************************
replace zimmer_cont = . if zimmer == .
gen zimmer_cont2 = zimmer_cont^2

gen baujahr_adopt = baujahr_kat
recode baujahr_adopt ( 1930 1950 1960 1970 = 1900) (1995 1990 = 1977) (2014 2008 = 2002)
gen modern_adopt = modern_kat
recode modern_adopt (1990 = 1977) (2015 2008 = 2002)


global merkat baujahr_adopt modern_adopt objektkat qualitaet heizart
global merkat_i ""
foreach x of global merkat {
	global merkat_i $merkat_i i.`x' 
}
global merkont grundflaeche wohnflaeche zimmer_cont grundflaeche2 wohnflaeche2 


***************************
***************************
* Matching
***************************
***************************

set seed 20181026
gen random = runiform()

keep if anfang_monat_geo >= monthly("2013 01", "YM")

cap drop matchid
gen matchid = ""

foreach num of numlist `=monthly("2013 01", "YM")'(1)`=monthly("2015 08", "YM")' {
	disp "Month: " `num' 
	sort random
	psmatch2 complier $merkat_i $merkont i.anbietertyp if anfang_monat_geo == `num' & time==anfang_monat_geo, noreplacement 

	replace _n1 = _id if complier==0 & _weight==1 & anfang_monat_geo == `num' & time==anfang_monat_geo
	gen aux1 = string(_n1)
	gen aux2 = string(anfang_monat_geo)
	replace matchid = aux2 + "_" + aux1 if _weight==1 & anfang_monat_geo==`num' & time==anfang_monat_geo
	drop aux1 aux2
	drop _*
}

* code matchid und time_disclosure correctly
bysort geo: egen aux = mode(matchid)
replace matchid = aux if missing(matchid)	
drop aux

bysort matchid time: egen aux = max(time_disclosure) if !missing(matchid)
replace time_disclosure = aux if missing(time_disclosure)
drop aux


keep geo time matchid time_disclosure
drop if missing(matchid)
save "$WORKDATA/result_psmatch.dta", replace
